package com.zw.util;

import com.zw.exception.StudyException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtility {

    public final static String CHARSET = "UTF-8";

    //设置此变量的目的是考虑到win环境存入数据库，url会有转义问题
    public final static String fileSeparator = "/";
    private static Log log = LogFactory.getLog(CommonUtility.class);


    static public JSONObject convertBeanToJsonObject(Object bean) throws StudyException {
        if (bean == null) {
            return new JSONObject();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return new JSONObject(objectMapper.writeValueAsString(bean));
        } catch (JSONException e) {
            throw new StudyException(e);
        } catch (IOException e) {
            throw new StudyException(e);
        }

    }

    static public JSONArray convertBeanToJsonArray(Object bean) throws StudyException {
        if (bean == null) {
            return new JSONArray();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return new JSONArray(new String(objectMapper.writeValueAsBytes(bean), "UTF-8"));
        } catch (Exception e) {
            throw new StudyException(-1, "服务器内部错误", e);
        }
    }


    public static String constructResultJson(String code, Object message) throws StudyException {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("code", code);
            jsonObject.put("message", message);
        } catch (JSONException e) {
            throw new StudyException(-1, "服务器内部错误", e);
        }
        return jsonObject.toString();
    }

    public static Map<String,Object> getResultJsonMap(String code, Object message)throws Exception {
    	Map<String,Object> map = new HashMap<String, Object>();
        try {
        	map.put("code", code);
        	map.put("message", message);
        } catch (Exception e) {
            throw new StudyException(-1, "服务器内部错误", e);
        }
        return map;
    }
    public static JSONObject constructFailureJson(int code) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("code", code);
        } catch (JSONException e) {
        	log.error(e.getMessage());
            return null;
        }
        return jsonObject;
    }

    public static <T> T convertJsonTo0bject(String json, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(json, valueType);
        } catch (Exception e) {
            log.error("json 转对象失败 json：" + json);
            return null;
        }
    }

    static public String convertBeanToString(Object bean) throws StudyException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(bean);
        } catch (IOException e) {
            throw new StudyException(e);
        }
    }
    
    /**
     * 按指定大小，分隔集合，将集合按规定个数分为n个部分
     * @param list
     * @param len
     * @return
     */
    public static List<List<String>> splitList(List<String> list, int len) {
    	if (list == null || list.size() == 0 || len < 1) {  
    		return null;  
    	}  
    	List<List<String>> result = new ArrayList<List<String>>();
    	  
    	int size = list.size();  
    	int count = (size + len - 1) / len;  
    	  
    	for (int i = 0; i < count; i++) {  
	    	List<String> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
	    	result.add(subList);  
    	}  
    	return result;  
	}
}
